#ifndef TABLEPRINTWIDGET_H
#define TABLEPRINTWIDGET_H

#include <QMainWindow>
#include <QtWidgets>
#include <QTableView>
#include "jsonpath.h"

class Ui_tablePrintWidget;
class lds_tablewidget;
class tablePrintWidget : public QDialog
{
    Q_OBJECT
public:
    enum _UType {
        _u_pre_table =            Qt::UserRole + 1,
        _u_pre_img_type =         Qt::UserRole + 2,
        _u_img =                  Qt::UserRole + 3,
        _u_pre_content_rcount =   Qt::UserRole + 4,
        _u_content_key =          Qt::UserRole + 5,
        _u_pre_key =              Qt::UserRole + 6,
        _u_header_key =           Qt::UserRole + 7,
    };


public:
    tablePrintWidget(QWidget *parent = nullptr);
    ~tablePrintWidget();

    void add(int row, QString text, const QString &u1, QStringList buttons = {"del", "up", "down", "add"});
    void addLine(int row);
    void setButton(int row, QStringList buttons = {"del", "up", "down", "add"});
    void clearButton(int row);
    void swapSections(int row1, int row2);

    QByteArray toJson();
    void formJson(JsonPathReader &r);

    void load();
    void loadInit();
    void setImage(int row, QString path);
    void setqrCode(int row, QString path);
    void findAndResetqrCode(QByteArray code);
    void setSpans(int row, int *_w = 0, int *_column = 0);

    QPushButton *createMinButton(const QString &text);
    void glayoutAddWidget(QString key, QGridLayout *layout, QWidget *w, int row, int column,
                          const char *signal, QObject *target, const char *member);

    void headerEnd();

    QModelIndex headerIndex();
    QModelIndex contentIndex();

    void add_receipt_head(int &row, JsonPathReader &r);
    void add_sale_head(int &row, JsonPathReader &r);
    void add_pay_detail(int &row, JsonPathReader &r);
    void add_pay_detail_paymode(int &row, JsonPathReader &r);
    void add_receipt_tail(int &row, JsonPathReader &r);
    void add_sale_detail(JsonPathReader &r);
    void add_sale_detail_content(JsonPathReader &r);

    static QPixmap scalePixmap(QModelIndex index, int cell_width);

    static void drawDocText(QPainter *painter, QFont font, QRect r, QString text);

    QPixmap printerPixmap(bool forTest = false);
    void saveJson();

    void headerSwitch(int r, int c, int r1, int c1);

    int wordWrapHeight(QString text, int w, QFont font);

    void initRecipt();
public slots:
    void toDel();
    void toUp();
    void toDown();
    void toAdd();

    void toActionAdd(QAction *a);

    void toAddItem(int row, QString table, QString key);
    void toCheckedItem(QStringList orders, QString table, QString key, bool checked);

    void headercheck(bool);
    void headermove0(bool);
    void headermove1(bool);
    void headerrow0(bool);
    void headerrow1(bool);
    void headerwidth(QString);
    void headerapp2content(bool);

    void vheaderapp2content(lds_tablewidget *header);

    void toKeepHeaderWidht();

    void toSetFont(QFont f);

    void emitImageReresh();
public slots:
    void on_pushButton_toJson_clicked();

    void on_pushButton_reset_clicked();

    void on_pushButton_adjust_clicked();

    void on_pushButton_fromJson_clicked();

    void on_pushButton_pix_clicked();

    void on_pushButton_select_font_clicked();

    void on_pushButton_printer_clicked();

    void tocolumn_switch();

    void tosettings();
signals:
    void imgRefresh(QPixmap pixmap);
public:
    static QString saveFilePath;
    static QString fastfd_defaultSetting;
public:
    Ui_tablePrintWidget *ui;
    lds_tablewidget *header;
    lds_tablewidget *content;
    QWidgetAction *headerAc;

    QMap<QObject *, QString > headerWidgets;

public:
    static QMap<QString, QVariant> receipt_head;
    static QMap<QString, QVariant> sale_head;
    static QMap<QString, QVariant> sale_detail;
    static QMap<QString, QVariant> sale_detail_content;
    static QMap<QString, QVariant> pay_detail;
    static QMap<QString, QVariant> pay_detail_paymode;

    static QMap<QString, QVariant> receipt_tail;

    static QMap<QString, QString> receiptTrans;
public:
    static QMap<QString, QVariant> colorMap;
    static QMap<QString, QVariant> receiptMap;
public:
    static void initreceiptMap();
};

class contentItemDelegate : public QStyledItemDelegate
{
    Q_OBJECT
public:
    contentItemDelegate(QObject *parent) : QStyledItemDelegate(parent){}


    // painting
    void paint(QPainter *painter,
               const QStyleOptionViewItem &option,
               const QModelIndex &index) const;
};

class txtItemDelegate : public QStyledItemDelegate
{
    Q_OBJECT
public:
    txtItemDelegate(QObject *parent) : QStyledItemDelegate(parent){}


    // painting
    void paint(QPainter *painter,
               const QStyleOptionViewItem &option,
               const QModelIndex &index) const;
};

#endif // TABLEPRINTWIDGET_H
